<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/1
 * Time: 16:28
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class LoginLog extends Model
{
    protected $table = 'login_log';
    protected $fillable = ['member_id', 'date', 'nums'];
    public $timestamps = false;

    //访问记录
    public function addLogin($member_id)
    {
        $today = date('Y-m-d');
        $sql = LoginLog::select('nums')
            ->where(['member_id'=> $member_id,'date'=>$today])
            ->first();
        if(!empty($sql)) {
            $sql->nums += 1;
            $res = $sql->save();
            return $res ? 'update' : false;
        }else {
            $insert = [
                'member_id' => $member_id,
                'from_source' => getIp(),
                'date' => $today,
                'nums' => 1,
            ];
            $res = LoginLog::create($insert);
        }
        return $res ? 'insert' : false;
    }
}